/// 1. 创建一个空String
/// 2. 通过字面值创建一个String
/// 2.1 通过String::from()
/// 2.2 使用str的方式
/// 3. 更新String
/// 3.1 push_str
/// 3.2 push
/// 3.3 使用“+”合并字符串
/// 3.4 使用format
/// 4. String索引
/// 5. str索引
/// 6. 遍历
/// 6.1 chars
/// 6.2 bytes
fn main() {
    // 创建空字符串
    let mut s1 = String::new();
    s1.push_str("hello"); // 这里也是更新字符串
    println!("s1 = {}", s1);

    // 通过字面值创建一个String
    let s2 = String::from("init something");
    println!("s2 = {}", s2);
    let s2 = "initial contents".to_string();
    println!("s2 = {}", s2);

    // 更新字符串
    let mut s3 = String::from("hello");
    s3.push_str(" world"); // 直接更新
    let ss = "!!!".to_string();
    s3.push_str(&ss); // 通过定义的变量更新
    println!("s3 = {}", s3);

    let mut s4 = String::from("tea");
    s4.push('m'); //这个只能加一个字符，且只能用单引号，有多个字符或者双引号都会报错
    println!("s4 = {}", s4);

    // 合并字符串
    let s5 = "hello".to_string();
    let s6 = String::from("world!");
    let s7 = s5 + &s6;
    println!("s7 = {}", s7);

    // format 合并字符串
    let s8 = String::from("tic");
    let s9 = String::from("tac");
    let s10 = String::from("toe");
    let s11 = format!("{}-{}-{}", s8, s9, s10); // format！和println！类似
    println!("s11 = {}", s11);
    println!("s8 = {}", s8);
    println!("s9 = {}", s9);
    println!("s10 = {}", s10);

    // 索引
    let s11 = String::from("hello");
    // let s12 = s11[5]; // String 不支持索引
    println!("s12 = {}", s11.len());

    let s12 = String::from("中国");
    println!("s12 = {}", &s12[0..3]);

    // 遍历chars
    for c in s12.chars() {
        println!("s12 chars遍历：{}", c);
    }
    // 遍历bytes
    for b in s12.bytes() {
        println!("s12 bytes遍历：{}", b)
    }

    // 统计字数
    let a = count_chinese_chars(&s11);
    println!("a = {}", a);

    let a = "总共12个".to_string().chars().count();
    println!("a = {}", a);
}
fn count_chinese_chars(text: &str) -> usize {
    text.chars().count()
}